﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Trip_Food_Entity : EntityBase<TRIP_FOOD>
    {
        public override RESP_Info Add(TRIP_FOOD model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into TRIP_FOOD(TRIP_ID,FOOD_ID) values (@TRIP_ID,@FOOD_ID)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", model.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("FOOD_ID", model.FOOD_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加行程餐饮成功</p></h5>";
                    info.Url = "TripFoodList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加行程餐饮失败</p></h5>";
                    info.Url = "TripFoodAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加行程餐饮失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(TRIP_FOOD model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update TRIP_FOOD set FOOD_ID=@FOOD_ID where TRIP_ID=@TRIP_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", model.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("FOOD_ID", model.FOOD_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑行程餐饮成功</p></h5>";
                    info.Url = "TripFoodList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑行程餐饮失败</p></h5>";
                    info.Url = "TripFoodAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑行程餐饮失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(TRIP_FOOD tripFood)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from TRIP_FOOD where TRIP_ID=@TRIP_ID AND FOOD_ID=@FOOD_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", tripFood.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("FOOD_ID", tripFood.FOOD_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除行程餐饮成功</p></h5>";
                    info.Url = "TripFoodList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除行程餐饮失败</p></h5>";
                    info.Url = "TripFoodList.aspx";

                    Tom.Net.Log.LogManager.Error("删除行程餐饮失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override TRIP_FOOD GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select TRIP_ID,FOOD_ID from TRIP_FOOD where TRIP_ID=@TRIP_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        TRIP_FOOD tripFood = new TRIP_FOOD();
                        if (!reader.IsDBNull(0)) tripFood.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tripFood.FOOD_ID = reader.GetString(1);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return tripFood;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程餐饮数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override TRIP_FOOD GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        TRIP_FOOD tripFood = new TRIP_FOOD();
                        if (!reader.IsDBNull(0)) tripFood.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tripFood.FOOD_ID = reader.GetString(1);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return tripFood;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程餐饮数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<TRIP_FOOD> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<TRIP_FOOD> tripFoods = new List<TRIP_FOOD>();
                    while (reader.Read())
                    {
                        TRIP_FOOD tripFood = new TRIP_FOOD();
                        if (!reader.IsDBNull(0)) tripFood.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tripFood.FOOD_ID = reader.GetString(1);

                        tripFoods.Add(tripFood);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return tripFoods;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程餐饮数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}

